import { defineStore } from "pinia";
import type { userInfoInter } from '@/types/user';
import { ref } from 'vue';
import { userInfoService } from '@/api/user';

export const useUserInfoStore = defineStore('userInfo', () => {
  let userInfo = ref<userInfoInter>({
    id: 0,
    username: '',
    nickname: '',
    email: '',
    userPic: '',
    createTime: '',
    updateTime: ''
  })
  const setUserInfo = (value: userInfoInter) => {
    userInfo.value = value
  }
  async function fetchUserInfo() {
    try {
      let result = await userInfoService();
      if (result && result.data.data) {
        setUserInfo(result.data.data);
        // console.log(result.data.data);
      } else {
        console.error('数据为空');
      }
    } catch (error) {
      console.error('获取用户信息失败:', error);
    }
  }
  const removeUserInfo = () => {
    userInfo.value = {
      id: 0,
      username: '',
      nickname: '',
      email: '',
      userPic: '',
      createTime: '',
      updateTime: ''
    }
  }

  return {
    userInfo,
    setUserInfo,
    fetchUserInfo,
    removeUserInfo
  }
}, { persist: true })